package com.servlet;

import com.dao.EmployeesDao;
import com.dao.impl.EmployeesDaoImpl;
import com.google.gson.Gson;
import com.vo.ResponseData;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;

/*
 *
 *@author XCH
 *@date2025 - 02 - 24
 *
 */
@WebServlet("/api/employees/delete")
public class DeleteServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        EmployeesDao employeesDao = new EmployeesDaoImpl();
        int row = employeesDao.delete(id);
        ResponseData responseData = new ResponseData();
        responseData.setMsg("删除成功");
        if (row != 1) {
            responseData.setMsg("删除失败");
            responseData.setCode(500);
        }
        String json = new Gson().toJson(responseData);
        resp.setContentType("application/json;charset=utf-8");
        resp.setCharacterEncoding("utf-8");
        PrintWriter out = resp.getWriter();
        out.print(json);
        out.flush();
        out.close();
    }
}
